import os
from typing import List, Optional

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    # 应用设置
    APP_NAME: str = "神算子API服务"
    APP_VERSION: str = "0.1.0"
    APP_DESCRIPTION: str = "基于FastAPI的后端服务"
    
    # 服务器设置
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    RELOAD: bool = True
    
    # 安全设置
    SECRET_KEY: str = "your-secret-key-here"  # 生产环境中应该使用环境变量
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # CORS设置
    CORS_ORIGINS: str = "*"  # 默认允许所有来源
    
    # 数据库设置 (未来可能会用到)
    DATABASE_URL: Optional[str] = None
    
    # Deepseek API设置
    DEEPSEEK_API_KEY: str = "sk-30275386e0b04b39a779f0ec4031bfa3"
    DEEPSEEK_API_BASE: str = "https://api.deepseek.com/v1"
    DEEPSEEK_MODEL: str = "deepseek-chat"
    
    # 百度搜索API设置
    BAIDU_SEARCH_API_KEY: str = "bce-v3/ALTAK-SZww9MNnkMrZaQGWNQZRR/11355ef4ac2d8df4f5e8f537e077db7d7e0bf405"  # 请替换为你的百度API密钥

    os.environ["OPENAI_API_KEY"] = DEEPSEEK_API_KEY

    # Milvus设置
    MILVUS_HOST: str = "localhost"
    MILVUS_PORT: int = 19530
    MILVUS_COLLECTION: str = "documents"
    
    # 日志级别配置
    LOG_LEVEL: str = "INFO"  # 全局日志级别
    AUDIO_LOG_LEVEL: str = "INFO"  # 音频相关日志级别，默认DEBUG，可配置
    LLM_LOG_LEVEL: str = "DEBUG"  # 大模型相关日志级别，默认DEBUG，可配置
    LANGCHAIN_LOG_LEVEL: str = "DEBUG"  # LangChain相关日志级别
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True

    @property
    def cors_origins_list(self) -> List[str]:
        """将CORS_ORIGINS字符串转换为列表"""
        if self.CORS_ORIGINS == "*":
            return ["*"]
        return [origin.strip() for origin in self.CORS_ORIGINS.split(",") if origin.strip()]

# 创建Settings实例
settings = Settings()
